package community.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import community.dto.AccessTokenDto;
import community.dto.GithubUser;
import community.provider.GithubProvider;

@Controller
public class AuthorizeController {
	@Autowired
	private GithubProvider githubProvider;

	@GetMapping("/callback")
	public String callback(@RequestParam(name="code") String code,
			@RequestParam(name="state") String state) { 
		AccessTokenDto accessTokenDto = new AccessTokenDto();
		accessTokenDto.setClient_id("0908ca56ca763b5ed8a1");
		accessTokenDto.setClient_secret("ab8d7e75212ac53b4722a5c23b28c5e14454b0ec");
		accessTokenDto.setCode(code);
		accessTokenDto.setRedirect_url("http://localhost:8887/callback");
		accessTokenDto.setState(state);
		String accessToken = githubProvider.getAccessToken(accessTokenDto);
		GithubUser user = githubProvider.getUser(accessToken);
		System.out.println(user.getName());
		return "index";
	}
}
